package com.approval.approvalapp.index.dbsx.wsys;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WsysBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/5/9.
 */

public class WsysAdapter extends RecyclerView.Adapter<WsysAdapter.Holder> {

    List<WsysBean> wsysBeanList;
    Context context;
    public void setWsysBeanList(List<WsysBean> wsysBeanList) {
        this.wsysBeanList = wsysBeanList;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_wsys,parent,false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.tv1.setText("申报事项: " + wsysBeanList.get(position).getITEM_NAME());

        holder.tv5.setText("申报时间: " + wsysBeanList.get(position).getSUBMIT_TIME());

        holder.tv4.setText("申请人: " + wsysBeanList.get(position).getAPPLICANT());

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wsysBeanList.get(position).getIs_sign().equals("1")){
                    Toast.makeText(context, "该办件已被其他人领取", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent();
                    intent.setClass(context, WsysXqActivity.class);
                    intent.putExtra("projID", wsysBeanList.get(position).getPROJID());
                    intent.putExtra("SUBMIT_TIME", wsysBeanList.get(position).getSUBMIT_TIME());
                    context.startActivity(intent);
                }
            }
        });


        if (wsysBeanList.get(position).getIs_sign().equals("1")){
            holder.iv.setVisibility(View.GONE);
            holder.state.setText("已领取");
            holder.state.setTextColor(Color.parseColor("#1465FD"));
        }else {
            holder.iv.setVisibility(View.VISIBLE);
            holder.state.setText("");
        }



    }

    @Override
    public int getItemCount() {
        if (wsysBeanList == null){
            return 0;
        }else {
            return wsysBeanList.size();
        }
    }

    class  Holder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv1)
        TextView tv1;
        @Bind(R.id.tv3)
        TextView tv3;
        @Bind(R.id.tv4)
        TextView tv4;
        @Bind(R.id.tv5)
        TextView tv5;
        @Bind(R.id.root)
        LinearLayout root;
        @Bind(R.id.state)
        TextView state;
        @Bind(R.id.iv)
        ImageView iv;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
